//
//  UIBarButtonItem+Extension.swift
//  CXCWeibo
//
//  Created by cxc on 15/6/29.
//  Copyright © 2015年 china. All rights reserved.
//

import UIKit

extension UIBarButtonItem {
    convenience init(imageName:String,highlightedImageName:String?){
        let btn = UIButton()
        btn.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        /// ?? 表示如果前面的为nil 就用后面的数据
        let highImageName = highlightedImageName ?? imageName + "_highlighted"
        
        btn.setImage(UIImage(named: highImageName), forState: UIControlState.Highlighted)
        btn.sizeToFit()
        self.init(customView:btn)
    }
    convenience init(imageName: String, highlightedImageName: String?, target: AnyObject?, actionName: String?) {
        
        let btn = UIButton()
        
        btn.setImage(UIImage(named: imageName), forState: UIControlState.Normal);
        
        let hImageName = highlightedImageName ?? imageName + "_highlighted"
        btn.setImage(UIImage(named: hImageName), forState: UIControlState.Highlighted)
        
        btn.sizeToFit()
        
        // 添加监听方法
        if actionName != nil {
            btn.addTarget(target, action: Selector(actionName!), forControlEvents: UIControlEvents.TouchUpInside)
        }
        
        self.init(customView: btn)
    }
}